package com.webchat.admin.controller;


import com.webchat.admin.service.OfficialArticleService;
import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.config.annotation.ValidatePermission;
import com.webchat.common.enums.RoleCodeEnum;
import com.webchat.common.helper.SessionHelper;
import com.webchat.domain.vo.request.publicaccount.SaveArticleRequestVO;
import com.webchat.domain.vo.response.publicaccount.ArticleBaseResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin-service/official/article")
public class OfficialArticleController {

    @Autowired
    private OfficialArticleService officialArticleService;

    /**
     * 提交文章
     *
     * @return
     */
    @ValidatePermission(role = RoleCodeEnum.ADMIN)
    @PostMapping("/submit")
    public APIResponseBean<Long> submit(@RequestBody SaveArticleRequestVO saveArticleRequest) {
        // TODO 参数校验
        String userId = SessionHelper.getCurrentUserId();
        saveArticleRequest.setAuthor(userId);
        return APIResponseBeanUtil.success(officialArticleService.submit(saveArticleRequest));
    }

    /**
     * 文章列表查询接口
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ValidatePermission(role = RoleCodeEnum.ADMIN)
    @GetMapping("/page")
    public APIPageResponseBean<ArticleBaseResponseVO> page(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                                 @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return officialArticleService.page(pageNo, pageSize);
    }
}
